/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;

import org.starsource.forum.client.domain.CategoryObject;
import org.starsource.forum.client.handler.SearchHandler;
import org.starsource.forum.client.handler.SessionCreateHandler;
import org.starsource.forum.client.handler.SessionNavigationHandler;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple widget representing prev/next page navigation.
 */
class SessionListToolbar extends Composite {
	@UiTemplate("SessionListToolbar.ui.xml")
	interface Binder extends UiBinder<Widget, SessionListToolbar> { }
	private static final Binder binder = GWT.create(Binder.class);

	@UiField TextBox searchBox;
	@UiField Anchor postButton;
	@UiField Element countLabel;
	@UiField Anchor newerButton;
	@UiField Anchor newestButton;
	@UiField Anchor oldestButton;
	@UiField Anchor olderButton;
	@UiField Button searchButton;

	private CategoryObject categoryObject;

	public void setCategory(CategoryObject category) {
		this.categoryObject = category;
		if (category == null || category.getChildren().size() > 0) {
			this.setVisibility(postButton, false);
		} else {
			this.setVisibility(postButton, true);
		}
	}

	private SessionNavigationHandler sessionNavigationHandler;

	public void setSessionNavigationHandler(SessionNavigationHandler sessionNavigationHandler) {
		this.sessionNavigationHandler = sessionNavigationHandler;
	}

	private SessionCreateHandler sessionCreateHandler;

	public void setSessionCreateHandler(SessionCreateHandler sessionCreateHandler) {
		this.sessionCreateHandler = sessionCreateHandler;
	}

	private SearchHandler searchHandler = new SearchHandler() {

		@Override
		public void onSearch(String keywords) {
			// TODO Auto-generated method stub

		}

	};
	public void setSearchHandler(SearchHandler searchHandler) {
		this.searchHandler = searchHandler;
	}

//	private Timer timer = null;
//	private void scheduleSearch() {
//		if (timer != null) {
//		timer.cancel();
//		}
//		timer = new Timer() {
//
//			@Override
//			public void run() {
//				SessionListToolbar.this.searchHandler.onSearch(searchBox.getText());
//			}
//
//		};
//		timer.schedule(2000);
//	}
	public SessionListToolbar() {
		initWidget(binder.createAndBindUi(this));
//		searchBox.addKeyUpHandler(new KeyUpHandler() {
//
//			@Override
//			public void onKeyUp(KeyUpEvent event) {
//				scheduleSearch();
//			}
//
//		});
	}

	public void update(int startIndex, int count, int max) {
		setVisibility(newestButton, startIndex != 0);
		setVisibility(newerButton, startIndex != 0);
		setVisibility(olderButton,
				startIndex + SessionList.VISIBLE_EMAIL_COUNT < count);
		setVisibility(oldestButton,
				startIndex + SessionList.VISIBLE_EMAIL_COUNT < count);
		countLabel.setInnerText("" + (startIndex + 1) + " - " + max + " of "
				+ count);
	}

	@UiHandler("searchBox")
	void onSearchBoxKeyPress(KeyPressEvent event) {
		if (event.getCharCode() == KeyCodes.KEY_ENTER) {
			SessionListToolbar.this.searchHandler.onSearch(searchBox.getText());
		}
	}

	@UiHandler("searchButton")
	void onSearchButtonClicked(ClickEvent event) {
//		if (timer != null) {
//			timer.cancel();
//			}
		SessionListToolbar.this.searchHandler.onSearch(searchBox.getText());
	}

	@UiHandler("newerButton")
	void onNewerClicked(ClickEvent event) {
		sessionNavigationHandler.newer();
	}

	@UiHandler("newestButton")
	void onNewestClicked(ClickEvent event) {
		sessionNavigationHandler.newest();
	}

	@UiHandler("oldestButton")
	void onOldestClicked(ClickEvent event) {
		sessionNavigationHandler.oldest();
	}

	@UiHandler("olderButton")
	void onOlderClicked(ClickEvent event) {
		sessionNavigationHandler.older();
	}

	@UiHandler("postButton")
	void onPostClicked(ClickEvent event) {
		SessionCreateDialog sessionCreateDialog = new SessionCreateDialog();
		sessionCreateDialog.setCategoryObject(categoryObject);
		sessionCreateDialog.setSessionCreateHandler(sessionCreateHandler);
		sessionCreateDialog.focus();
		sessionCreateDialog.center();
	}
	private void setVisibility(Widget widget, boolean visible) {
		widget.getElement().getStyle().setVisibility(
				visible ? Visibility.VISIBLE : Visibility.HIDDEN);
	}
}
